<?php

/**
 * Holds class Mvc_Response
 *
 * @author Mark
 *
 * @package Library
 * @subpackage Mvc
 */

/**
 * Final response
 *
 * @author Mark
 */
class Mvc_Response {

	/**
	 * Body
	 * @var string $_body
	 */
	protected $_body = '';

	/**
	 * Headers
	 * @var array $_header
	 */
	protected $_header = array();

	/**
	 * HTTP status code
	 * @var integer $_status
	 */
	protected $_status;

	/**
	 * Constructor
	 *
	 * @return void
	 */
	public function __construct() {
		$this->setStatus(200);
	}

	/**
	 * Returns HTTP status code
	 *
	 * @return integer
	 */
	public function getStatus() {
		return $this->_status;
	}

	/**
	 * Sets body
	 *
	 * @param string $body
	 * @return Mvc_Response (fluent interface)
	 */
	public function setBody($body) {
		$this->_body = $body;
		return $this;
	}

	/**
	 * Sets header
	 *
	 * @param string $type
	 * @param strng $value
	 * @return Mvc_Response (fluent interface)
	 */
	public function setHeader($type, $value) {
		$this->_header[$type] = $value;
		return $this;
	}

	/**
	 * HTTP status code
	 *
	 * @param integer $status
	 * @return Mvc_Response (fluent interface)
	 */
	public function setStatus($status) {
		$this->_status = $status;
		return $this;
	}

	/**
	 * Returns string representation
	 *
	 * @return string
	 */
	public function __toString() {
		try {
			// Set HTTP status code
			header('HTTP/1.1 ' . $this->_status);

			// Headers
			foreach($this->_header as $type => $value) {
				header($type . ': ' . $value);
			}
		}catch(Exception $e){
		}

		// Body
		return $this->_body;
	}

}
